/*
 * Copyright © 2019 by MaShibo. All Rights Reserved.
 */

package mashibo.project.etc.assistant.activity;

import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.os.Bundle;
import android.util.Log;

import com.obu.util.EncodingUtil;

import java.io.IOException;

import mashibo.project.etc.assistant.R;

public class NfcTestActivity extends BaseActivity {

    private static final String TAG = "NfcTestActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc_test);
        Intent intent = getIntent();
        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            Log.d(TAG, "NFC TechList: " + tag.toString());
            IsoDep isoDep = IsoDep.get(tag);
            if (isoDep == null) {
                return;
            }
            try {
                isoDep.connect();
                try {

                    //选择文件
                    byte[] rsp1 = isoDep.transceive(EncodingUtil.StringToBytes("00A40000021001"));
                    String hexRsp1 = EncodingUtil.bytesToHexString(rsp1);
                    Log.d(TAG, "onCreate: select file response " + hexRsp1);
                    //读取卡片信息
                    byte[] rsp2 = isoDep.transceive(EncodingUtil.StringToBytes("00B095002B"));
                    String hexRsp2 = EncodingUtil.bytesToHexString(rsp2);
                    Log.d(TAG, "onCreate: read cardInfo " + hexRsp2);
                    //读取卡片余额
                    byte[] rsp3 = isoDep.transceive(EncodingUtil.StringToBytes("805C000204"));
                    String hexRsp3 = EncodingUtil.bytesToHexString(rsp3);
                    Log.d(TAG, "onCreate: read balance " + hexRsp3);
                } finally {
                    isoDep.close();
                }

            } catch (IOException e) {
                Log.d(TAG, "onCreate: isoDep.connect()", e);
            }

        }
    }
}
